using System;
using System.Collections.Generic;
using System.Linq;
using Android.Content;
using Android.Text;
using Android.Views;
using Android.Widget;
using Android.Graphics;

namespace jivemessenger
{
    public class ConversationArrayAdapter : ArrayAdapter<ChatBubble>
    {

        private TextView _chatBubbleContentTextView;

        private TextView _chatBubbleNameTextView;
        private readonly List<ChatBubble> _bubbles = new List<ChatBubble>();
        //private LinearLayout _wrapper;



        public ConversationArrayAdapter(Context context, int textViewResourceId)
            : base(context, textViewResourceId)
        {

        }

        public List<ChatBubble> GetChatBubbleList()
        {
            return _bubbles;
        }

        public void AddChatBubble(ChatBubble obj)
        {
            this._bubbles.Add(obj);
            //super.add(object);
            base.Add(obj);
            if (_bubbles.Count > 250)
            {
                ChatBubble toRemove = this._bubbles.ElementAt(0);
                base.Remove(toRemove);
                this._bubbles.RemoveAt(0);
            }
            //NotifyDataSetChanged();
        }
        public int GetChatBubbleCount()
        {
            return this._bubbles.Count();
        }
        
        public ChatBubble GetChatBubble(int index)
        {
            return this._bubbles[index];
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View row = convertView;
            if (row == null)
            {
                LayoutInflater inflater = (LayoutInflater)this.Context.GetSystemService(Context.LayoutInflaterService);
                row = inflater.Inflate(Resource.Layout.ChatBubbleListItem, parent, false);
            }
            LinearLayout root = (LinearLayout)row.FindViewById(Resource.Id.chatBubbleLayoutRoot);
            //this._wrapper = (LinearLayout)row.FindViewById(Resource.Id.chatBubbleWrapper);

            ChatBubble bubble = this.GetChatBubble(position);

            this._chatBubbleContentTextView = (TextView)row.FindViewById(Resource.Id.chatBubbleText);

            this._chatBubbleContentTextView.SetText(bubble.Comment, TextView.BufferType.Normal);
            if (bubble.User.Color != null)
                this._chatBubbleContentTextView.SetTextColor(Color.ParseColor("#" + bubble.User.Color));
            //SpannableString spanString = new SpannableString(tempString);
            //if(bubble.User.FontStyle== "Bold")
            //    this._chatBubbleContentTextView.bold
            this._chatBubbleNameTextView = (TextView)row.FindViewById(Resource.Id.chatBubbleName);
            this._chatBubbleNameTextView.SetText(String.Format("{0} ({1})\n", bubble.User.PrettyName, bubble.Timestamp.ToShortTimeString()), TextView.BufferType.Normal);

            // wrapper.SetBackgroundResource(bubble.Left ? Resource.Drawable.bubble_yellow : Resource.Drawable.bubble_green);
            //  root.SetGravity(bubble.Left ? GravityFlags.Left : GravityFlags.Right);

            return row;
        }

        //public Bitmap decodeToBitmap(byte[] decodedByte)
        //{
        //    return BitmapFactory.DecodeByteArray(decodedByte, 0, decodedByte.Length);
        //}

    }
}